
import {config} from './config';
import md5 from './md5';
import {CusBASE64} from './base64';
import logs from './log';
var baseURL = config.baseURL

/**
 * 生成sign
 */
var self
function request(api, method, header, params, success,errMsg){
	// params.app_key=appKey
	var tou={}
	let token = uni.getStorageSync('token');
	if(token!=""){
		// params.token=token
		tou={Authorization: token}
	}
    
	const requestTask = uni.request({
        url: baseURL + api,
        method: method,
        header: tou,
        data: params,
		dataType:"json",
        success: function(res) {
            if(res.data.code==1000){
				success(res.data)
			}else{
				if(errMsg){
					errMsg(res.data)
				}
			}
			
        },
        fail: function(){
			uni.hideLoading();
            uni.showToast({
                icon: "none",
                title: "网络连接异常，请稍后重试"
            })
        }
    })
	
}
// let token = uni.getStorageSync('token');
var GET_METHOD = "GET"
var HEADER

function get(api, params, success,errMsg){
    request(api, GET_METHOD, HEADER, params, success,errMsg)
}
var POST_METHOD = "POST"

function post(api, params, success,errMsg){
    request(api, POST_METHOD, HEADER, params, success,errMsg)
}
var DEL_METHOD = "DELETE"

function del(api, params, success,errMsg){
    request(api, DEL_METHOD, HEADER, params, success,errMsg)
}

function b64EncodeUnicode(str) {
    return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
        return String.fromCharCode('0x' + p1);
    }));
}

function b64DecodeUnicode(str) {
    return decodeURIComponent(atob(str).split('').map(function(c) {
        return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
    }).join(''));
}



module.exports = {
    GET: get,
    POST: post,
	DEL: del
}